//package com.plian.system.service.im.impl;
//
//import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
//import com.plian.Tools.*;
//import com.plian.Tools.pagedata.PageData;
//import com.plian.system.constant.sys.FormStatusConstant;
//import com.plian.system.constant.sys.FormTypeConstant;
//import com.plian.system.entity.im.PlanFiling;
//import com.plian.system.mapper.im.PlanFilingMapper;
//import com.plian.system.mybatis.base.BaseFlowService;
//import com.plian.system.mybatis.base.BaseServiceImpl;
//import com.plian.system.pdf.OutPdf;
//import com.plian.system.properties.GzwProperties;
//import com.plian.system.service.im.IPlanFilingService;
//import com.plian.system.service.sys.code.IFormCodeService;
//import com.plian.system.service.wf.FormWorkflowService;
//import com.plian.system.vo.im.PlanFilingVO;
//import com.plian.system.wrapper.im.PlanFilingWrapper;
//import lombok.AllArgsConstructor;
//import lombok.extern.slf4j.Slf4j;
//import org.apache.commons.io.FileUtils;
//import org.apache.commons.io.IOUtils;
//import org.apache.commons.lang.exception.ExceptionUtils;
//import org.apache.commons.lang3.StringUtils;
//import org.springframework.core.io.DefaultResourceLoader;
//import org.springframework.core.io.ResourceLoader;
//import org.springframework.stereotype.Service;
//import org.springframework.transaction.annotation.Transactional;
//
//import java.io.File;
//import java.io.InputStream;
//import java.text.SimpleDateFormat;
//import java.util.*;
//
//import static com.plian.system.constant.sys.FormStatusConstant.UNREVIEWED;
//
///**
// * 服务实现类
// *
// * @author
// * @since 2019-09-27
// */
//@Slf4j
//@Service
//@AllArgsConstructor
//public class PlanFilingServiceImpl extends BaseServiceImpl<PlanFilingMapper, PlanFiling> implements IPlanFilingService, BaseFlowService {
//
//	private FormWorkflowService formWorkflowService;
//
//	private IFormCodeService formCodeService;
//
//	private PlanFilingWrapper planFilingWrapper;
//
//	private GzwProperties gzwProperties;
//
//
//	@Override
//	@Transactional(rollbackFor=RuntimeException.class)
//	public boolean saveOrUpdate(PlanFiling planFiling) {
//
//		if (!Optional.ofNullable(planFiling.getStatus()).isPresent()) {
//			planFiling.setStatus(FormStatusConstant.UNREVIEWED);
//		}
//		if (!Optional.ofNullable(planFiling.getId()).isPresent()) {
//			planFiling.setCode(formCodeService.getCode(FormTypeConstant.IM_PLAN_FILING_FORM));
//			planFiling.setFormType(String.valueOf(FormTypeConstant.IM_PLAN_FILING_FORM));
//			planFiling.setVersion("1.0");
//		}
//		return super.saveOrUpdate(planFiling);
//	}
//
//	@Override
//	@Transactional(rollbackFor=RuntimeException.class)
//	public boolean deleteLogic(List<Long> ids) {
//		for (Long id : ids) {
//			PlanFiling planFiling = getById(id);
//			if (planFiling == null) {
//				return false;
//			} else if (planFiling.getStatus() != null &&
//					(planFiling.getStatus() != FormStatusConstant.STASH && planFiling.getStatus() != FormStatusConstant.UNREVIEWED)) {
//				return false;
//			}
//			formWorkflowService.deleteProcessInstance(String.valueOf(id));
//		}
//		return super.deleteLogic(ids);
//	}
//
//	@Override
//	public HashMap<String,Object> commit(String id) {
//		handleStatus(Long.valueOf(id), FormStatusConstant.FLOW);
//
//		//将需要提交的流程变量返回，用于工作流侧
//		return new HashMap<String,Object>();
//	}
//
//	@Override
//	public Boolean finish(String id) {
//		return handleStatus(Long.valueOf(id), FormStatusConstant.FINISH);
//	}
//
//	@Override
//	public Boolean reject(String id, int formState, String comment) {
//		return handleStatus(Long.valueOf(id), formState);
//	}
//
//	/**
//	 * 统一处理状态机事件方法
//	 *
//	 * @param id          主键
//	 * @param updateStatus  更新状态
//	 * @return boolean
//	 */
//	private Boolean handleStatus(Long id, int updateStatus) {
//		try {
//			PlanFiling planFiling = getById(id);
//			/**
//			 * 设置审批人和审批时间
//			 */
//			if (updateStatus == FormStatusConstant.FINISH){
//				if (StringUtil.isBlank(planFiling.getFilingCode())){
//					String code = formCodeService.getFilingCode(FormTypeConstant.IM_PLAN_FILING_FORM);
//					planFiling.setFilingCode(gzwProperties.getCodeStartName() + "规备〔" + code.substring(0,4) + "〕" + code.substring(code.length()-3) + "号");
//				}
//				planFiling.setApprovedUser(TokenUtil.getTokenUserId());
//				Date now = DateUtil.now();
//				planFiling.setApprovedTime(now);
//			}
//			planFiling.setStatus(updateStatus);
//			updateById(planFiling);
//			return true;
//		} catch (Exception e) {
//			log.error(ExceptionUtils.getFullStackTrace(e));
//			return false;
//		}
//	}
//
//
//	@Override
//	public String checkYear(PlanFiling planFiling) {
//		LambdaQueryWrapper<PlanFiling> queryWrapper = new LambdaQueryWrapper<>();
//		if (!Optional.ofNullable(planFiling.getFilingYear()).isPresent()) {
//			return "填报时间为空";
//		}
//		if (!Optional.ofNullable(planFiling.getCompanyId()).isPresent()){
//			return "填报单位为空";
//		}
//		queryWrapper.eq(PlanFiling::getCompanyId, planFiling.getCompanyId());
//		queryWrapper.eq(PlanFiling::getFilingYear, planFiling.getFilingYear());
//		PlanFiling one = getOne(queryWrapper);
//		if (!Optional.ofNullable(one).isPresent()){
//			return null;
//		}
//		if (one.getId().equals(planFiling.getId())){
//			return null;
//		}
//		return "该年份已填报";
//	}
//
//
//	@Override
//	public File buildPdf(Long id) {
//		PlanFilingVO bean = planFilingWrapper.entityToVO(getById(id));
//		File file = null ;
//
//		try {
//			ResourceLoader resourceLoader = new DefaultResourceLoader();
//			InputStream inputStream = resourceLoader.getResource("pdf/planfiling.pdf").getInputStream();
//			//通过将文件转换为临时文件进行操作
//			File model = File.createTempFile("model", ".png");
//			try {
//				FileUtils.copyInputStreamToFile(inputStream, model);
//			} finally {
//				//关闭IO
//				IOUtils.closeQuietly(inputStream);
//			}
//
//			if (bean!=null&&model.exists()) {
//				Map< String,Object > data = new HashMap<>(16);
//				data.put("companyId",bean.getCompanyName());
//				data.put("recordFileName",bean.getRecordFileName());
//				data.put("filingCode",bean.getFilingCode());
//				data.put("recordTotalInvestment",bean.getRecordTotalInvestment());
//				data.put("fixedAssetsInvestment",bean.getFixedAssetsInvestment());
//				data.put("equityInvestment",bean.getEquityInvestment());
//				data.put("otherInvestment",bean.getOtherInvestment());
//				data.put("filingOpinions",bean.getFilingOpinions());
//				data.put("recordFormReceiver",bean.getRecordFormReceiver());
//				data.put("remark",bean.getRemark());
//				file = OutPdf.createPdf(model, data, bean.getClass().getName());
//			}
//
//		} catch(Exception e) {
//			log.error(ExceptionUtils.getFullStackTrace(e));
//		}
//		return file;
//	}
//	private static String processDate(Date date){
//		SimpleDateFormat format = new SimpleDateFormat("yyyy年MM月dd日");
//		return date==null?"":format.format(date);
//	}
//	private static String processDate2(Date date){
//		SimpleDateFormat format = new SimpleDateFormat("yyyy");
//		return date==null?"":format.format(date);
//	}
//
//	@Override
//	public List<PageData> findByIds(List<String> formIds) {
//		LambdaQueryWrapper<PlanFiling> queryWrapper = new LambdaQueryWrapper<>();
//		queryWrapper.in(PlanFiling::getId, Func.toLongList(StringUtils.join(formIds, StringPool.COMMA)));
//		List<PlanFiling> planFilings = list(queryWrapper);
//		List<PlanFilingVO> planFilingVOS = planFilingWrapper.entityToVO(planFilings);
//		List<PageData> pageDataList = new ArrayList<>();
//		try{
//			if (CollectionUtil.isNotEmpty(planFilingVOS)){
//				for (PlanFilingVO vo : planFilingVOS){
//					PageData pd = new PageData();
//					pd.put("id", vo.getId());
//					pd.put("UNITNAME", vo.getCompanyName());
//					pd.put("APPLICANTNAME", vo.getCreateUserName());
//					pd.put("applicantId", vo.getCreateUser());
//					pd.put("applyOrganizationId", vo.getCompanyId());
//					pd.put("formType", FormTypeConstant.IM_PLAN_FILING_FORM);
//					pd.put("orgName", vo.getCompanyName());
//					pageDataList.add(pd);
//				}
//			}
//
//			return pageDataList;
//		} catch (Exception e) {
//			log.error(ExceptionUtils.getFullStackTrace(e));
//		}
//		return pageDataList;
//	}
//
//	@Override
//	public List<Integer> getFormType() {
//		return Arrays.asList(FormTypeConstant.IM_PLAN_FILING_FORM);
//	}
//
//	@Override
//	public void goBackToStash(String formId) {
//		handleStatus(Long.valueOf(formId), UNREVIEWED);
//	}
//}
